<?php


class Model_Account extends PhalApi_Model_NotORM {

    function getLastAccount($userId) {
        $where = [
            'user_id'=>$userId,
            'data_flag'=>1,
        ];
        $rowAccount = DI()->notorm->account->where($where)->order('id desc')->fetch();
        $rowAccount['amount'] = doubleval($rowAccount['amount']);
        $rowAccount['record_date'] = date('Y年m月d日', $rowAccount['record_stamp']);
        $rowAccount['detail'] = $this->getAccountDetail($rowAccount['id']);
        return $rowAccount;
    }

    public function checkAccountValid($userId, $accountId){
        $where = array(
            'user_id'=>$userId,
            'id'=>$accountId,
        );
        return DI()->notorm->account->where($where)->fetch();
    }

    public function addAccount($data)
    {
        $accountORM = DI()->notorm->account;
        $accountORM->insert($data);
        return $accountORM->insert_id();
    }

    public function addAccountDetail($data)
    {
        $detailORM = DI()->notorm->account_detail;
        $detailORM->insert($data);
        return $detailORM->insert_id();
    }

    function getAccountDetail($accountId){
        $where = [
            'account_id'=>$accountId,
            'data_flag'=>1,
        ];
        $rs = DI()->notorm->account_detail->where($where)->fetchAll();
        foreach($rs as $key=>$detail){
            $rs[$key]['amount'] = doubleval($detail['amount']);
            $rs[$key]['record_datetime'] = date('Y-m-d H:i:s', detail['record_stamp']);
        }
        return $rs;
    }

    function getAccountList($userId,$pageNum,$pageSize) {
        $start = intval(($pageNum-1) * $pageSize);
        $where = [
            'user_id'=>$userId,
            'data_flag'=>1
        ];
        $rowsAccount = DI()->notorm->account->where($where)->limit($start, $pageSize)->fetchAll();
        foreach($rowsAccount as $key=>$account){
            // 钱需要转换格式
            $rowsAccount[$key]['id'] = intval($account['id']);
            $rowsAccount[$key]['amount'] = doubleval($account['amount']);
            $rowsAccount[$key]['record_date'] = date('Y年m月d日', $account['record_stamp']);
            $accountId = $account['id'];
            $rowsAccount[$key]['detail'] = $this->getAccountDetail($accountId);
        }
        return $rowsAccount;
    }

    // 服务器备份 账本数据
    function saveAccount($userId, $accountJson, $detailJson, $note) {
        $accountJson = cleanJson($accountJson);
        $detailJson = cleanJson($detailJson);
        $accountData = json_decode($accountJson, true);
        $detailList = json_decode($detailJson, true);
        if ($accountData && $detailList) {
            $accountField = array(
                'amount' => $accountData['amount'],
                'note' => $note,
                'user_id' => $userId,
                'record_stamp' => time(),
            );
            $accountId = $this->addAccount($accountField);
            foreach($detailList  as $detail) {
                $detailField = array(
                    'account_id' => $accountId,
                    'category_id'=> $detail['category_id'],
                    'name' => $detail['name'],
                    'amount' => $detail['amount'],
                );
                $this->addAccountDetail($detailField);
            }
            return true;
        } else {
            return false;
        }
    }

    // 删除 Account —— 逻辑删除 data_flag = 0
    function deleteAccount($userId, $accountId) {
        $where = [
            'user_id' => $userId,
            'id' => $accountId,
        ];
        $data['data_flag'] = 0;
        $rs = DI()->notorm->account->where($where)->update($data);
        if ($rs == 0 || $rs === false) {
            return false;
        } else {
            return true;
        }
    }
}
